package router

import (
	"github.com/gin-gonic/gin"
	"github.com/robfig/cron/v3"
	"log"
	"myBlog/utils"
	"os"
	"path"
	"strconv"
	"strings"
)

func createMovieNote(c *gin.Context) {
	var note movieNote
	c.ShouldBindJSON(&note)
	stmt, err := db.Prepare("INSERT INTO movieNote " +
		"(`name`,`views`,`time`,`url`,`brief`)" +
		"values (?,?,?,?,?)")
	if err != nil {
		c.Error(err)
	}
	result, err := stmt.Exec(note.Name, note.Views, note.Time, note.Url, note.Brief)
	if err != nil {
		c.Error(err)
	}
	Nid, err := result.LastInsertId()
	if err != nil {
		c.Error(err)
	}
	c.JSON(200, gin.H{
		"id": Nid,
	})
}

func createMovieNoteComment(c *gin.Context) {
	var com movieNoteComment
	c.ShouldBindJSON(&com)
	stmt, err := db.Prepare("INSERT INTO movieNoteComment " +
		"(`movieNoteId`,`name`,`email`,`time`,`content`,`website`,`pname`,`pcontent`)" +
		"values (?,?,?,?,?,?,?,?)")
	if err != nil {
		c.Error(err)
	}
	println(com.Content, com.MovieNoteId)
	result, err := stmt.Exec(com.MovieNoteId, com.Name, com.Email, com.Time, com.Content, com.Website, com.Pname, com.Pcontent)
	if err != nil {
		c.Error(err)
	}
	Nid, err := result.LastInsertId()
	if err != nil {
		c.Error(err)
	}
	c.JSON(200, gin.H{
		"id": Nid,
	})

}

//可以携带参数year-->表示年份
func getMoiveNoteList(c *gin.Context) {
	year := c.Query("year")
	stmt, err := db.Prepare("SELECT  * FROM movieNote")
	if err != nil {
		c.Error(err)
	}
	rows, err := stmt.Query()
	if err != nil {
		c.Error(err)
	}
	movieNotes := make([]movieNote, 0)
	for rows.Next() {
		var note movieNote
		rows.Scan(&note.Id, &note.Name, &note.Views, &note.Time, &note.Url, &note.Brief)
		if len(year) != 0 && !utils.CheckYear(note.Time, year) {
			continue
		}
		movieNotes = append(movieNotes, note)
	}
	c.JSON(200, gin.H{
		"list": movieNotes,
	})
}

func searchMovie(c *gin.Context) {
	name := c.Query("name")
	stmt, err := db.Prepare("SELECT * FROM movieNote WHERE name like ?")
	if err != nil {
		c.Error(err)
	}
	rows, err := stmt.Query(name)
	if err != nil {
		c.Error(err)
	}
	notes := make([]movieNote, 0)
	for rows.Next() {
		var note movieNote
		rows.Scan(&note.Id, &note.Name, &note.Views, &note.Time, &note.Url, &note.Brief)
		notes = append(notes, note)
	}
	println(notes)
	c.JSON(200, gin.H{
		"list": notes,
	})
}

func getMovieNoteCommentLis(c *gin.Context) {
	stmt, err := db.Prepare("select * from movieNoteComment")
	if err != nil {
		c.Error(err)
	}
	rows, err := stmt.Query()
	if err != nil {
		c.Error(err)
	}
	comments := make([]movieNoteComment, 0)
	for rows.Next() {
		var com movieNoteComment
		rows.Scan(&com.Id, &com.MovieNoteId, &com.Name, &com.Email, &com.Time, &com.Content, &com.Website, &com.Pname, &com.Pcontent)
		comments = append(comments, com)
	}
	c.JSON(200, gin.H{
		"list": comments,
	})
}

func getMovieNoteCommentListById(c *gin.Context) {
	pid := c.Query("id")
	t, err := strconv.Atoi(pid)
	if err != nil {
		c.Error(err)
	}
	id := int64(t)
	stmt, err := db.Prepare("select * from movieNoteComment where `movieNoteId` = ?")
	if err != nil {
		c.Error(err)
	}
	rows, err := stmt.Query(id)
	if err != nil {
		c.Error(err)
	}
	comments := make([]movieNoteComment, 0)
	for rows.Next() {
		var com movieNoteComment
		//一定要全部读出来！！！这里id不读其他数据也读不出来
		rows.Scan(&com.Id, &com.MovieNoteId, &com.Name, &com.Email, &com.Time, &com.Content, &com.Website, &com.Pname, &com.Pcontent)
		comments = append(comments, com)
	}
	c.JSON(200, gin.H{
		"list": comments,
	})
}

func getMovieNoteById(c *gin.Context) {
	id := c.Query("id")
	stmt, _ := db.Prepare("SELECT * FROM movieNote WHERE `id` = ?")
	rows, err := stmt.Query(id)
	if err != nil {
		c.Error(err)
	}
	var note movieNote
	if rows.Next() {
		rows.Scan(&note.Id, &note.Name, &note.Views, &note.Time, &note.Url, &note.Brief)
	}
	for rows.Next() {
	}

	stmt, _ = db.Prepare("UPDATE movieNote set `views` = ? where `id` = ?")
	_, err = stmt.Exec(note.Views+1, id)
	if err != nil {
		c.Error(err)
	}
	c.JSON(200, gin.H{
		"movieNote": note,
	})
}

func delMovieNoteById(c *gin.Context) {
	id := c.Query("id")
	stmt, err := db.Prepare("DELETE FROM movieNote WHERE `id` = ?")
	if err != nil {
		c.Error(err)
	}
	_, err = stmt.Exec(id)
	if err != nil {
		c.Error(err)
	}
	c.JSON(200, gin.H{})
}

func delMovieNoteCommentById(c *gin.Context) {
	id := c.Query("id")
	stmt, err := db.Prepare("DELETE FROM movieNoteComment WHERE `id` = ?")
	if err != nil {
		c.Error(err)
	}
	_, err = stmt.Exec(id)
	if err != nil {
		c.Error(err)
	}
	c.JSON(200, gin.H{})
}

func InitMovie(r *gin.Engine) {
	g := r.Group("/movie")
	g.POST("/createMovieNote", createMovieNote)
	g.POST("/createMovieNoteComment", createMovieNoteComment)
	g.POST("/getMoiveNoteList", getMoiveNoteList)
	g.POST("/searchMovie", searchMovie)
	g.POST("/getMovieNoteCommentListById", getMovieNoteCommentListById)
	g.POST("/getMovieNoteById", getMovieNoteById)

	//定时更新 最新修改时间
	timer := cron.New()
	_, err := timer.AddFunc("@every 1m", ScanupdateModifyTime)
	if err != nil {
		println(err.Error())
	}
	timer.Start()
}

func ScanupdateModifyTime() {
	//fmt.Println("update Movie Note Modify Time")
	pwd, _ := os.Getwd()
	nPath := utils.DealPath(path.Join(pwd, "movie"))
	files := utils.GetAllThing(nPath)
	for _, value := range files {
		updateModifyTime(value)
	}
}

func updateModifyTime(note utils.File) {
	if note.IsDir {
		return
	}
	time, _ := utils.GetUpdateTime(utils.DealPath(note.Path))
	stmt, err := db.Prepare("UPDATE movieNote set `time` = ? where `name` = ?")
	if err != nil {
		log.Fatal(err)
	}
	name := strings.Split(note.Name, ".")[0]
	//println(name,time)
	_, err = stmt.Exec(time, name)
	if err != nil {
		log.Fatal(err)
	}
}
